package com.lzl.service;

import com.lzl.constant.CommonStatusEnum;
import com.lzl.dto.PassengerUser;
import com.lzl.responese.ResponseResult;
import com.lzl.mapper.PassengerUserMappper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * //TODO
 *
 * @author zhaolinlin
 * @description //TODO
 * @createTime 2022/7/31 18:25
 */
@Service
public class UserService {
    @Autowired
    PassengerUserMappper passengerUserMappper;

    public ResponseResult loginOrRegister(String passengerPhone){
        //根据手机号查询用户信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);
        List<PassengerUser> passengerUsers = passengerUserMappper.selectByMap(map);

        //判断用户是否存在
        //如果用户不存在，创建用户
      if(passengerUsers.size()== 0) {
          PassengerUser passengerUser = new PassengerUser();
          LocalDateTime now = LocalDateTime.now();
          passengerUser.setGmtCreate(now);
          passengerUser.setGmtModified(now);
          passengerUser.setPassengerGender((byte)0);
          passengerUser.setPassengerName("lzl");
          passengerUser.setPassengerPhone(passengerPhone);
          passengerUser.setState((byte)0);

          passengerUserMappper.insert(passengerUser);
      }

      return ResponseResult.success();

    }

    public ResponseResult getUserByPhone(String phone){
        HashMap<String, Object> map = new HashMap<>();
        map.put("passenger_phone",phone);
        List<PassengerUser> passengerUsers = passengerUserMappper.selectByMap(map);
        if(passengerUsers.size() == 0) {
            return ResponseResult.fail(CommonStatusEnum.USER_NOT_EXISTS.getCode(), CommonStatusEnum.USER_NOT_EXISTS.getValue());
        }else{
            PassengerUser passengerUser = passengerUsers.get(0);
            return ResponseResult.success(passengerUser);
        }
    }
}
